package com.ruoyi.party.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.party.system.domain.Member;
import com.ruoyi.party.system.service.IMemberService;
import com.ruoyi.party.system.service.IOrgService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 党员基本信息Controller
 * 
 * @author 黄宝康
 * @date 2021-04-16
 */
@Controller
@RequestMapping("/business/member")
public class MemberController extends BaseController
{
    private String prefix = "business/member";

    @Autowired
    private IMemberService memberService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysPasswordService passwordService;
    @Autowired
    private ISysRoleService roleService;
    
    @Autowired
    private IOrgService orgService;

    @RequiresPermissions("business:member:view")
    @GetMapping()
    public String member(ModelMap mmap)
    {
    	 mmap.put("orgZbs",orgService.selectOrgZbList());
    	 mmap.put("orgDxzs",orgService.selectOrgDxzList());
        return prefix + "/member";
    }

    /**
     * 查询党员基本信息列表
     */
    @RequiresPermissions("business:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Member member)
    {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }
    @PostMapping("/listmembers/{orgId}")
    @ResponseBody
    public TableDataInfo listmembers(@PathVariable("orgId") String orgId)
    {
    	startPage();
    	Member member = new Member();
    	member.setOrgId(orgId);
    	List<Member> list = memberService.selectMemberList(member);
    	return getDataTable(list);
    }

    /**
     * 导出党员基本信息列表
     */
    @RequiresPermissions("business:member:export")
    @Log(title = "党员基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Member member)
    {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        return util.exportExcel(list, "党员基本信息数据");
    }

    /**
     * 新增党员基本信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	 mmap.put("orgZbs",orgService.selectOrgZbList());
    	 mmap.put("orgDxzs",orgService.selectOrgDxzList());
    	 mmap.put("roles", roleService.selectRoleAll().stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        return prefix + "/add";
    }

    /**
     * 新增保存党员基本信息
     */
    @RequiresPermissions("business:member:add")
    @Log(title = "党员基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Member member)
    {
    	// 增加党员，同时增加用户
    	/**
    	 * 必填项，用户名称：userName
    	 * 登录账号：loginName
    	 * 登录密码：password
    	 * 初始化角色：普通党员
    	 */
    	SysUser u = new SysUser();
    	u.setUserName(member.getMemberName());
    	u.setLoginName(member.getMemberId());
    	u.setPassword(member.getMemberPwd());
    	u.setSalt(ShiroUtils.randomSalt());
    	u.setPassword(passwordService.encryptPassword(u.getLoginName(), u.getPassword(), u.getSalt()));
        u.setCreateBy(ShiroUtils.getLoginName());
        u.setRoleIds(member.getRoleIds());
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(u.getLoginName())))
        {
            return error("新增用户'" + u.getLoginName() + "'失败，登录账号已存在");
        }
        userService.insertUser(u);
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改党员基本信息
     */
    @GetMapping("/edit/{memberId}")
    public String edit(@PathVariable("memberId") String memberId, ModelMap mmap)
    {
        Member member = memberService.selectMemberById(memberId);
        mmap.put("member", member);
        mmap.put("orgZbs",orgService.selectOrgZbList());
   	 	mmap.put("orgDxzs",orgService.selectOrgDxzList());
        return prefix + "/edit";
    }

    /**
     * 修改保存党员基本信息
     */
    @RequiresPermissions("business:member:edit")
    @Log(title = "党员基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Member member)
    {
    	// 修改党员，同时修改用户，目前只有用户useName,角色，党员编号，和密码不能修改
    	SysUser user = new SysUser();
    	user.setLoginName(member.getMemberId());
    	user.setUserName(member.getMemberName());
    	user.setUpdateBy(ShiroUtils.getLoginName());
    	userService.updateUserMemberInfo(user);
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除党员基本信息
     */
    @RequiresPermissions("business:member:remove")
    @Log(title = "党员基本信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(memberService.deleteMemberByIds(ids));
    }
}
